import Network from './network';
import kkui from 'kkui/kkui';

const apiCfg = WebCfg.api;
const baseURL = apiCfg['url'] + ':' + apiCfg['port'] + '/webapi';
const network = new Network(baseURL, kkui);

const api = {

  //全局字典
  globalDict: {
    url: '/index/roadConditionNo'
  },
  // 用户相关
  user: {
    login: {
      // url: '/oauth/token',
      url: '/sysusers/login',
      // isFormData: 1
    },

    checkToken: {
      url: '/sysusers/isToken',
      method: 'post'
    },
    editPw: {
      url: 'sysusers/editPassword'
    },
    list: {
      url: '/sysusers/selectSysUser'
    },
    add: {
      url: '/sysusers/savaSysUser'
    },
    edit: {
      url: '/sysusers/updateSysUser'
    },
    del: {
      url: '/sysusers/deleteSysUser'
    },
    importTable: {
      url: '/sysusers/importSysUser',
      isFormData: true
    },
    downTemplate: {
      url: '/dict/downloadTemplate',
      needUrl: true
    }

  },
  // 角色相关
  role: {
    list: {
      url: '/role/selectRoleList'
    },
    add: {
      url: '/role/save'
    },
    edit: {
      url: '/role/save'
    },
    delete: {
      url: '/role/delete'
    },
    getGroupList:{
      url: '/role/getGroupList'
    }
  },
  // 菜单相关
  menu: {
    list: {
      url: '/menu/getTreeMenuList'
    },
    getNewMenu:{
      url: '/menu/getNewMenu'
    }
  },
  // 单位相关
  company: {
    getName: {
      url: '/sysusers/selectUserAdmin'
    }
  },

  // 摄像机相关
  camera: {
    list: {
      url: '/camera/list'
    },
    fault: {
      url: '/camera/queryFaultInfo'
    },
    faultTable: {
      url: '/bizeventinfo/exportEventInfo',
      responseType: 'blob'
    },
    faultTable2: {
      url: '/bizeventinfo/exportYkList',
      responseType: 'blob'
    },
    exportTable: {
      url: 'camera/exportCamera',
      responseType: 'blob'
    },
    jihe: {
      url: '/camera/updateJh'
    },
    fabu: {
      url: '/camera/updateFb'
    },
    hl: {
      url: '/camera/updateHl'
    },
    orderInfo: {
      url: '/eventInfoDispatch/queryOrderList2'
    },
    examine: {
      url: '/eventInfoDispatch/passage2'
    }
  },

  // 首页
  index: {
    timedData: {
      url: '/index/newIndex',
      isFormData: 1
    }
  },

  // 路情
  monitor: {
    liveEvt: {
      url: '/bizeventinfo/newInspectEvent'
    },
    liveEvt2: {
      url: '/bizeventinfo/newInspectEvent2'
    },
    ignoreMsg: {
      url: '/sysusers/knowEvent'
    },
    list: {
      url: '/bizeventinfo/list'
    },
    list3: {
      url: '/bizeventinfo/ykList'
    },
    list2: {
      url: '/bizeventinfo/lists'
    },
    today: {
      url: '/index/dayInfo'
    },
    evtHandle: { // 事件处理
      jihe: {
        url: '/bizeventinfo/updateJh',
      },
      fabu: {
        url: '/bizeventinfo/updateFb'
      },
      hulve: {
        url: '/bizeventinfo/updateHl'
      }
    },
    multEvtHandle: {
      jihe: {
        url: '/bizeventinfo/updateBatchJh',
      },
      fabu: {
        url: '/bizeventinfo/updateBatchFb'
      },
      hl: {
        url: '/bizeventinfo/updateBatchHl'
      }
    },
    orderInfo: {
      url: '/eventInfoDispatch/queryOrderList'
    },
    examine: {
      url: '/eventInfoDispatch/passage'
    }


  },

  // 巡检
  patrol: {
    cars: {
      list: {
        url: '/dict/center_class'
      },
      add: {
        url: '/dict/save_center_class'
      },
      edit: {
        url: '/dict/update_center_class'
      },
      del: {
        url: '/dict/delete_center_class'
      }
    }
  },
  // 组织架构
  org: {
    company: {
      url: '/user/selectCompany',
    },
    department: {
      url: '/user/selectDepartment',
    },
    user: {
      url: '/user/selectPeople'
    },

    userList: {
      url: '/user/selectUser'
    },

    addUser: {
      url: '/user/savaUser'
    },
    renewUser: {
      url: '/user/updateUser'
    },
    delUser: {
      url: '/user/deleteUser'
    },
    userTemplate: {
      url: '/dict/downloadTemplate',
      needUrl: true
    },
    importUserTable: {
      url: '/user/importUser',
      isFormData: true
    },

    unitName: {
      url: '/sysusers/updateUserAdmin',
      isFormData: true
    },
    list: {
      // url: '/apiDept/index',
      url: 'apiDept/list',
      method: 'post'
    },
    renewOrg: {
      url: '/apiDept/edit',
      method: 'post'
    },
    delOrg: {
      url: '/apiDept/deletes',
      append: true
    },
    addOrg: {
      url: '/apiDept/add',
      method: 'post'
    },
  },

  data: {
    dictList: {
      url: '/dict/dict_class'
    }
  },

  handle: {
    list: {
      url: '/eventInfoDispatch/queryEventInfoDistribution'
    },
    count: {
      url: '/eventInfoDispatch/queryEventInfoCount'
    },
    sendOrder: {
      url: '/eventInfoDispatch/insertHandle'
    },
    returnOrder: {
      url: '/eventInfoDispatch/returnHandle'
    },
    startOrder: {
      url: '/eventInfoDispatch/resultHandle'
    },
    endOrder: {
      url: '/eventInfoDispatch/completionHandle'
    },
    uploadImg: {
      url: '/uploads/uploadEventImg/files',
      isFormData: true
    },
    getNote: {
      url: '/eventInfoDispatch/queryEventInfoDistribution'
    }
  },
  count: {
    allRoad: {
      url: '/camera/queryRoadName'
    },
    queryRoad:{
      url:'/camera/queryEventCount'
    },
    queryEvent:{
      url:'/bizeventinfo/Time'
    }
  }
};


const server = (apiUrl, data, needLoading) => {
  let idx = 0;
  if (needLoading) idx = kkui.loading.show();
  let cfg = null;

  if (apiUrl === 'sysLog') {
    cfg = {
      url: (data.type === 'login' ? '/apiLoginLog/index' : '/apiOperLog/index') + '?operName=' + data.username + '&username=' + data.username + '&page=' + data.page + '&limit=' + data.limit,
      method: 'get'
    }
  } else {
    apiUrl.split('.').forEach(name => {
      cfg = cfg ? cfg[name] : api[name]
    });
    if (data) cfg = {...cfg, data};

    if (cfg.append) {
      cfg.url += cfg.data;
      delete cfg.data
    }
  }

  return new Promise(function (resolve, reject) {
    network.ajax(cfg, rst => {
      if (needLoading) kkui.loading.hide(idx);

      if (rst.code === 0 && cfg.needUrl) {
        resolve(baseURL + '/' + rst['data'][0]);
        return false;
      }

      if (rst.code === 0 || rst['access_token']) {
        resolve(rst['access_token'] || rst['data']);
      } else if (cfg.responseType) {
        resolve(rst);
      } else {
        kkui.alert({title: '出错啦！', cont: rst.msg || rst.message});
        reject(rst.msg);
      }
    })
  })
};


export default server;
